import plugin from '../../lib/plugins/plugin.js'
import { segment } from "oicq";
import fetch from 'node-fetch'
import cfg from '../../lib/config/config.js'
const _path = process.cwd();
export class example extends plugin {
  constructor () {
    super({
      /** 功能名称 */
      name: '成语接龙',
      /** 功能描述 */
      dsc: '简单开发示例',
      /** https://oicqjs.github.io/oicq/#events */
      event: 'message',
      /** 优先级，数字越小等级越高 */
      priority: 5000,
      rule: [
        {
          /** 命令正则匹配 */
          reg: '^成语接龙$',
          /** 执行方法 */
          fnc: 'cyjl'
        },
        {
          /** 命令正则匹配 */
          reg: '^我接.*$',
          /** 执行方法 */
          fnc: 'answerCheck'
        },
        {
          /** 命令正则匹配 */
          reg: '^结束成语接龙$',
          /** 执行方法 */
          fnc: 'EndCheck'
        }
      ]
    })
  }

 
  async cyjl (e) {
   
  let guessConfig = getGuessConfig(e)
  if (guessConfig.gameing) {
    e.reply('成语接龙正在进行哦!')
    return true;
  }
  
   let dm = await(await fetch(`https://xiaoapi.cn/API/cyjl.php?id=${e.user_id}&msg=开始成语接龙`)).text(); 


if(dm.includes("赢")){
        e.reply(dm)
        e.reply(`成语接龙结束,请重新开始`);
         return true;
       }



  e.reply(dm+`发送【我接+成语】,我才有反应噢`,true);
   
  guessConfig.gameing = true;
  guessConfig.current = e.user_id;

    guessConfig.timer = setTimeout(() => {
      if (guessConfig.gameing) {
        guessConfig.gameing = false;
        e.reply(`嘿嘿,成语接龙结束拉,这局是纳西妲赢了噢！`);
     
		return true;
      }
    }, 120000)//毫秒数


  return true; //返回true 阻挡消息不再往下
  }
  
 async  answerCheck(e){
     let guessConfig = getGuessConfig(e);
    let {gameing, current } = guessConfig;
    
   
    
   let es = e.msg.replace(/我接/g,"").trim();
    let sz = await(await fetch(`https://xiaoapi.cn/API/cyjl.php?id=${guessConfig.current}&msg=我接${es}`)).text(); 
   
   if(gameing){

        e.reply(sz)

       if(sz.includes("赢了")){
        e.reply(`成语接龙结束`);
         guessConfig.gameing = false
         clearTimeout(guessConfig.timer);
         return true;
       }
   }
 }
 
 async EndCheck(e){
     let guessConfig = getGuessConfig(e);
    let {gameing, current } = guessConfig;
    
   
        if(gameing){
         guessConfig.gameing = false
         clearTimeout(guessConfig.timer);
         
         
         e.reply(`成语接龙已结束`);
         
         return true;
    }else{
        e.reply(`成语接龙游戏都没开始,你结束锤子呢？`)
        return true;
    }
 }
}

const guessConfigMap = new Map()

function getGuessConfig(e) {
    let key = e.message_type + e[e.isGroup ? 'group_id' : 'user_id'];
    let config = guessConfigMap.get(key);
    if (config == null) {
      config = {
        gameing: false,
        current: '',
        timer: null,
      }
      guessConfigMap.set(key, config);
    }
    return config;
  }
